package com.c1.client.dialog.input;

import com.c1.client.Frame;
import com.c1.client.dialog.BaseButton;
import com.c1.client.dialog.JDialogButtonSupport;
import com.c1.client.dialog.input.button.*;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

/**
 * @author zhangjiangnan
 */
@Slf4j
public class InputDialog extends JDialogButtonSupport implements ActionListener {

    private static final int WIDTH = Frame.WIDTH * 2 / 3;
    private static final int HEIGHT = Frame.HEIGHT * 2 / 3;

    private static final String TITLE = "个股查询";
    private static final String FONT_NAME = "Serif";
    private static final int FONT_STYLE = Font.PLAIN;
    private static final int FONT_SIZE = 20;

    private static volatile InputDialog me;

    public static InputDialog me() {
        if (me == null) {
            synchronized (InputDialog.class) {
                if (me == null) {
                    me = new InputDialog();
                }
            }
        }
        return me;
    }

    private JTextArea textArea;
    private final JCheckBox isExcludeModeCheckBox;
    private final JCheckBox isExcludeCanNotBuy;
    private final JTextField dateTextField;

    public boolean isExcludeMode() {
        return isExcludeModeCheckBox.isSelected();
    }

    public boolean isExcludeCanNotBuy() {
        return isExcludeCanNotBuy.isSelected();
    }

    public String getTextAreaText() {
        return textArea.getText().trim();
    }

    public String getDateText() {
        return dateTextField.getText().trim();
    }

    @Override
    public List<Class<? extends BaseButton>> getBaseButton() {
        return Arrays.asList(
                InputGoButton.class,
                InputCancelButton.class);
    }

    private InputDialog() {
        super();
        this.setActionListener(this);

        setModal(true);

        initTextArea();

        add(new JScrollPane(textArea), BorderLayout.CENTER);

        JPanel queryPanel = new JPanel(new BorderLayout());
        JPanel datePanel = new JPanel();

        isExcludeModeCheckBox = new JCheckBox("排除逻辑");
        datePanel.add(isExcludeModeCheckBox);
        isExcludeCanNotBuy = new JCheckBox("排除不可买");
        datePanel.add(isExcludeCanNotBuy);

        dateTextField = new JTextField(6);
        JLabel dateLabel = new JLabel("日期:");
        datePanel.add(dateLabel);
        datePanel.add(dateTextField);

        queryPanel.add(datePanel, BorderLayout.WEST);
        queryPanel.add(getCommandArea(), BorderLayout.EAST);

        add(queryPanel, BorderLayout.SOUTH);

        setFocusable(true);
        setTitle(TITLE);

        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
    }

    private JPanel getCommandArea() {
        JPanel commandArea = new JPanel();
        commandArea.add(get(InputGoButton.class));
        commandArea.add(get(InputCancelButton.class, KeyEvent.VK_C));
        return commandArea;
    }

    private void initTextArea() {
        textArea = new JTextArea();
        textArea.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        process(command);
    }
}